package com.laoye.controller;
import com.laoye.bean.Cart;
import com.laoye.bean.Product;
import com.laoye.dto.*;
import com.laoye.service.AliyunOSSService;
import com.laoye.service.ProductService;
import com.laoye.util.Page;
import com.laoye.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private AliyunOSSService aliyunOSSService;

    /**
     * 更新商品库存，每次付款后才更新
     * @param productStoreDTO 商品信息
     * @return 返回空
     */
    @PutMapping("updateStore")
    public Result<String> updateStore(@RequestBody ProductStoreDTO productStoreDTO){
        productService.updateStore(productStoreDTO);
        return Result.success("商品库存更新");
    }
    @GetMapping("getSimpleProducts")
    public Result<List<ProdSimpleDTO> > getSimpleProducts( @RequestParam(required = false) String prod_name,
                                                           @RequestParam(required = false) String category) {
        List<ProdSimpleDTO> simpleProducts = productService.getSimpleProductByCategory( category, prod_name);
        return Result.success(simpleProducts);
    }


    @GetMapping("getStore/{id}")
    public Result<Integer> getStore(@PathVariable("id") Integer id){
        System.out.println("商品库存"+productService.getStore( id));
        return Result.success("", productService.getStore(id));
    }
    /**
     * 获取商品信息
     *
     * @param id 商品id
     * @return 返回商品信息
     */
    @GetMapping("get/{id}")
    public Result<ProductInfoDTO> getProduct(@PathVariable("id") Integer id){
        if (id == null)
            return Result.error("商品id不能为空");
        return Result.success("", productService.getProductById(id));
    }

/**
     * 获取商品基本信息列表，展现在用户主界面
     *
     * @return 返回商品列表
     */
    @GetMapping("getSimpleProduct")
    public Result<List<ProdSimpleDTO>> getSimpleProduct(){
        return Result.success("", productService.getSimpleProduct());
    }
/**
     * 修改商品状态
     *
     * @param statusDTO 商品状态信息
     * @return 返回修改成功
     */
    @PutMapping("updateStatus")
    public Result<String> updateProduct(@RequestBody StatusDTO statusDTO){
        productService.updateStatus(statusDTO);
        return Result.success("商品状态修改成功");
    }

    /**
     * 添加商品
     *
     * @param product 商品信息
     * @return 返回添加成功
     */
    @PostMapping("add")
    public Result<String> addProduct(@RequestBody Product product){
        productService.addProduct(product);
        return Result.success("商品添加成功");
    }

    /**
     * 商品图上传
     *
     * @param file 商品图像文件
     * @return 返回上传成功，并返回上传后的文件路径
     */
    @PostMapping("avatar")
    public Result<String> avatarUpload(@RequestParam("file") MultipartFile file) throws Exception {
        String uploadFile = aliyunOSSService.uploadFile(file);
        return Result.success("", uploadFile);

    }

    @PutMapping("update")
    public Result<String> updateProduct(@RequestBody Product product){
        productService.updateProduct(product);
        return Result.success("商品修改成功");
    }

    @GetMapping("getProducts")
    public Result<Page<Product>> getProducts(@RequestParam(value = "prod_name",required = false) String prod_name,
                                                 @RequestParam(value = "category",required = false) String category,
                                                 @RequestParam(value = "status",required = false) Byte status,
                                                 @RequestParam("pageNum") Integer pageNum,
                                                 @RequestParam("pageSize") Integer pageSize){
        Page<Product> products = productService.getAllProducts(prod_name, category, status, pageNum, pageSize);
        return Result.success("", products);
    }
}
